package lua.compiler.tokens;
import lua.compiler.CompilerException;
import lua.compiler.CtsType;
import lua.compiler.Function;

import org.antlr.runtime.tree.Tree;

public class ReturnToken extends GenericToken {

	@Override
	public void parseImpl(Tree tree, Function function) throws CompilerException {
		// czy zwracamy void
		if (tree.getChildCount() == 0) {
			function.addInstruction("ret");
			function.setReturnType(tree, CtsType.VOID);
			return;
		}	
		// parsujemy zwracane wyrazenie
		// po ExpToken.parse zostaje na stosie - czyli zostaje zwrocone
		ExpToken expToken = new ExpToken();
		expToken.parse(tree.getChild(0), function);
		CtsType expType = expToken.getCtsType();
		
			
		function.setReturnType(tree, expType);
		
		if (expType.isNumeric())
			expType.ConvertTo(function.getReturnType(), function);
		
		function.addInstruction("ret");
	}
}